<?php
namespace app\admin\service;

use app\common\model\DrawRecord;
use app\common\model\Exchange;
use app\common\model\PieceShare;
use app\common\model\Product;
use app\common\model\SharePage;
use app\lib\exception\BaseException;

//产品统计
class ProductStatistic
{
    public $data;
    public $start_time;
    public $end_time;
    public $model;
    public $store_id;
    public $products;

    public function __construct($store_id = 0)
    {
        $this->store_id = $store_id;//店铺
        $this->model = new Exchange();
        $products = Product::field('id,name')->select();
        if (!$products) throw new BaseException(['msg' => '没有产品！']);
        $this->products = $products;
    }

    public function setStore($store_id)
    {
        $this->$store_id = $store_id;
    }

    public function setTime($start_date,$end_date)
    {
        if (empty($start_date) || empty($end_date))
            throw new BaseException(['msg' => '起始时间或结束时间不能为空！']);
        $this->start_time = strtotime($start_date .' 0:0:0');
        $this->end_time = strtotime($end_date .' 23:59:59');
    }

    //按产品分类计算参与人数
    public function getJoinNum()
    {
        //分享和抽取都算参与游戏
        //获取分享的user_id
        $where = '1=1 ';
        if ($this->start_time && $this->end_time){
            $where .= ' AND create_time>='.$this->start_time.' AND create_time<='.$this->end_time;
        }
        $where .= ' AND product_id <> 0';
        $res = SharePage::where($where)->field('group_concat(distinct user_id) user_id,product_id')->group('product_id')->select();
        $res = collection($res)->toArray();
        $product_ids = [];
        $list = [];
        foreach ($res as $v) {
            $product_ids[] = $v['product_id'];
            $list[$v['product_id']] = [
                'product_id' => $v['product_id'],
                'user_ids' => explode(',', $v['user_id'])
            ];
        }
        //获取抽取的user_id
        $res = DrawRecord::where($where)->field('group_concat(distinct user_id) user_id,product_id')->group('product_id')->select();
        $res = collection($res)->toArray();
        $list1 = [];
        foreach ($res as $v) {
            $product_ids[] = $v['product_id'];
            $list1[$v['product_id']] = [
                'product_id' => $v['product_id'],
                'user_ids' => explode(',', $v['user_id'])
            ];
        }
        $return = [];
        $product_ids = array_unique($product_ids);

        foreach ($this->products as $v) {
            $number = 0;
            $user_ids = [];
            $user_ids += isset($list[$v['id']]) ? $list[$v['id']]['user_ids'] : [];
            $user_ids += isset($list1[$v['id']]) ? $list1[$v['id']]['user_ids'] : [];
            $return[] = [
                'name' => $v['name'],
                'product_id' => $v['id'],
                'user_num' => count($user_ids)
            ];
        }
        return $return;
    }

    //按照产品计算页面分享次数
    public function getSharePageNum()
    {
        //获取转发次数
        $where = '1=1 AND product_id <> 0';
        if ($this->start_time && $this->end_time){
            $where .= ' AND create_time>='.$this->start_time.' AND create_time<='.$this->end_time;
        }
        $res = SharePage::where($where)->field('product_id,count(1) as times')->group('product_id')->select();
//        if(!$res) throw new BaseException(['msg'=>'没有分享数据']);
        $res = collection($res)->toArray();
        $tmp = [];
        foreach ($res as $v) {
            $tmp[$v['product_id']] = $v;
        }
        $return = [];
        foreach ($this->products as $v) {
            $return[] = array_merge($v->toArray(), isset($tmp[$v['id']]) ? $tmp[$v['id']] : ['times'=>0]);
        }
        return $return;
    }

    //按照产品计算碎片分享次数
    public function getPieceShareNum()
    {
        $where = '1=1 AND product_id <> 0';
        if ($this->start_time && $this->end_time){
            $where .= ' AND create_time>='.$this->start_time.' AND create_time<='.$this->end_time;
        }
        $res = PieceShare::where($where)->field('product_id,count(1) as times')->group('product_id')->select();
//        if(!$res) throw new BaseException(['msg'=>'没有碎片分享数据']);
        $res = collection($res)->toArray();
        $tmp = [];
        foreach ($res as $v) {
            $tmp[$v['product_id']] = $v;
        }
        $return = [];
        foreach ($this->products as $v) {
            $return[] = array_merge($v->toArray(), isset($tmp[$v['id']]) ? $tmp[$v['id']] : ['times'=>0]);
        }
        return $return;
    }

    //根据产品获取兑换数量
    public function getExchangeAmount()
    {
        $where = '1=1 AND product_id <> 0 AND is_exchanged=1';
        //根据店铺划分
        if ($this->store_id) {
            $where .= " AND store_id = $this->store_id ";
        }
        if ($this->start_time && $this->end_time){
            $where .= ' AND create_time>='.$this->start_time.' AND create_time<='.$this->end_time;
        }
        $res = $this->model->where($where)->field('product_id,count(1) as number,SUM(cost) as amount')->group('product_id')->select();
//        if(!$res) throw new BaseException(['msg'=>'没有碎片分享数据']);
        $res = collection($res)->toArray();
        $tmp = [];
        foreach ($res as $v) {
            $tmp[$v['product_id']] = $v;
        }
        $return = [];
        foreach ($this->products as $v) {
            $return[] = array_merge($v->toArray(),
                isset($tmp[$v['id']]) ? $tmp[$v['id']] : ['number' => 0, 'amount' => 0]);
        }
        return $return;
    }

    //根据产品获取总支出
    public function getExpenses()
    {
        $where = '1=1 AND product_id <> 0 AND is_exchanged=1';
        //根据店铺划分
        if ($this->store_id) {
            $where .= "AND store_id = $this->store_id ";
        }
        if ($this->start_time && $this->end_time){
            $where .= ' AND create_time>='.$this->start_time.' AND create_time<='.$this->end_time;
        }
        $res = Exchange::where($where)->field('product_id,SUM(cost) as expenses')->group('product_id')->select();
        $res = collection($res)->toArray();
        print_r($res);
    }
    //
    //获取每日的兑换量
    public function getDailyExchange()
    {
        $where = '1=1';
        if ($this->store_id) {
            $where .= 'e.store_id=' . $this->store_id;
        }
        if ($this->start_time && $this->end_time){
            $where .= ' AND e.create_time>='.$this->start_time.' AND e.create_time<='.$this->end_time;
        }
        $where .= ' AND e.consume_date is not null';
        if ($this->start_time && $this->end_time) {
            $where .= ' AND e.consume_date>=' . $this->start_time . ' AND e.consume_date<=' . $this->end_time;
        }
        $field = 'sum(p.price) as total_price,
        e.consume_date,count(1) as count';
        $res = $this->model->alias('e')->join('product p', 'e.product_id = p.id')->field($field)->where($where)->group('consume_date')->select();
        $res = collection($res)->toArray();
        if (!$res) return [];
        $data = [];
        foreach ($res as $k => $v) {
            $data['date'][] = $v['consume_date'];
            $data['total_price'][] = $v['total_price'];
            $data['count'][] = $v['count'];
        }
        return $data;
    }

    //获取产品占比
    public function getProductProportion()
    {
        $where = '1=1';
        if ($this->store_id) {
            $where .= 'e.store_id=' . $this->store_id;
        }
        if ($this->start_time && $this->end_time){
            $where .= ' AND e.create_time>='.$this->start_time.' AND e.create_time<='.$this->end_time;
        }
        $where .= ' AND e.consume_date is not null';
        if ($this->start_time && $this->end_time) {
            $where .= ' AND e.consume_date>=' . $this->start_time . ' AND e.consume_date<=' . $this->end_time;
        }
        $field = 'sum(p.price) as total_price,
        count(1) as count';
        $res = $this->model->alias('e')->join('product p', 'e.product_id = p.id')->field($field)->where($where)->group('p.id')->select();
        $res = collection($res)->toArray();
        if (!$res) return [];
        $data = [];
        foreach ($res as $k => $v) {
            $data['total_price'][] = $v['total_price'];
            $data['count'][] = $v['count'];
        }
        return $data;
    }
}